const app = getApp()
Page({
  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    tabList: [],
    current: 0,
    offset: '0rpx',
    offsetTimeout: null,
    banner_list:{}
  },
  getOffset(){
      const offsetTimeout = setTimeout(() => {
        let query = wx.createSelectorQuery();
        query.select('.header').boundingClientRect(rect=>{
          try{
            let clientHeight = rect.height;
            let clientWidth = rect.width;
            let ratio = 750 / clientWidth;
            let height = clientHeight * ratio;
            this.setData({
              offset: `${height||0}rpx`
            })
          }catch(e){
            console.log("ERROR:获取分类页头部高度失败")
          }
        }).exec();
      }, 300)
      this.setData({offsetTimeout})
    
  },
  handleChange({ detail }){
    this.setData({
      current: detail.key
    })
    getApp().cateId='';
    this.GetCateIndex(this.data.current)
  },
  onLoad(){   
    this.GetCateIndex(0)
    setTimeout(()=>{
      this.selectComponent("#page").hideLoading()
      this.getOffset() // 计算头部高度
    },1000)
  },
  onShow(){   
    let  cateId=this.data.current;
    this.GetCateIndex(cateId)  
    this.getOffset() // 计算头部高度
  },
  onUnload(){
    if(this.data.offsetTimeout) clearTimeout(this.data.offsetTimeout)
  },
  onHide() {
    getApp().cateId='';
  },
  GetCateIndex(cateId){ 
    console.log('current',cateId) 
    let dataJson={
      cateId:cateId
    }
    if(getApp().cateId){
      dataJson.cateId=getApp().cateId      
    }

    this.CateIndex(dataJson).then(res=>{
      if(res.resCode=='9999'){
        this.setData({
          tabList:res.data,
          current:res.data.cate_id,
          banner_list:res.data.banner_list
        })
      }
    })
  },
  goodsall(e){
    let cateId=e.currentTarget.dataset.cateid
    this.router('/pagesShop/goodsall/index?cateId='+cateId)
  }
})